package com.group1.bookmanagement.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static String format(Date date) {
		return format(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static Date parse(String source) {
		return parse(source, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 时间转字符
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		if (date == null) {
			return null;
		}

		if (pattern == null || pattern.isEmpty()) {
			throw new IllegalArgumentException("时间格式不能为空");
		}

		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 字符转时间
	 * 
	 * @param source
	 * @param pattern
	 * @return
	 */
	public static Date parse(String source, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = null;

		try {
			date = sdf.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 获取当前年份
	 * @return
	 */
	public static String getCurrentYear() {
		Calendar date = Calendar.getInstance();
		String year = String.valueOf(date.get(Calendar.YEAR));
		return year;
	}
	
	/**
	 * 获取当前月份
	 * @return
	 */
	public static String getCurrentMonth() {
		Calendar date = Calendar.getInstance();
		String month = String.valueOf(date.get(Calendar.MONTH));
		return month;
	}
	
	/**
	 * 获取当前日期
	 * @return
	 */
	public static String getCurrentDay() {
		Calendar date = Calendar.getInstance();
		String day = String.valueOf(date.get(Calendar.DAY_OF_MONTH));
		return day;
	}
}
